<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<body>
		<h2>
			List of user's orders:
		</h2>
		<div class="gridContainer">
			<button dojoType="dijit.form.Button">
				Delete user order
				<script event="onClick" args="event" type="dojo/method">
					if ( dijit.byId( "userOrdersGrid").selection.getSelected( ).length == 0)
						return;
					var orderId = dijit.byId( "userOrdersGrid").selection.getSelected( )[0].orderId[0];
					dojo.xhrPost( {
						url : "/FinalProject/app/ordersManagement?ordersOp=order_delete&orderId=" + orderId,
						load : function( data, ioArgs) {
							if ( data == "ERROR") {
								dijit.byId( "toaster")
										.setContent( "Backend error deleting order. Please try later.", "fatal")
										.show( );
								return;
							}
							dijit.byId( "userOrdersGrid").removeSelectedRows( );
						}
					});
				</script>
			</button>
			<div id="userOrdersGrid" style="width:100%;height:350px">
			</div>
		</div>	
	</body>
	<script type="text/javascript">
		dojo.addOnLoad( function( ) {
			var userOrdersLayout = [
				{ field : "orderId", 	name: "Id", 				width : "auto"},
				{ field : "projectName",name: "Project name", 		width : "auto"},
				{ field : "date", 		name: "Order Date", 		width : "auto"},
				{ field : "itemType", 	name: "Instrument type", 	width : "auto"},
				{ field : "itemDescription", name: "Description", 	width : "auto"},
				{ field : "timeUnit", 	name : "Time slot length", 	width : "auto"},
				{ field : "usagePrice", name : "Price per slot", 	width : "auto"},
				{ field : "startSlot", 	name : "Start slot", 		width : "auto"},
				{ field : "slotsCount",	name : "Slots count", 		width : "auto"},
				{ field : "total", 		name : "Total price", 		width : "auto"}
			];
			var userOrdersGrid = new dojox.grid.DataGrid( {
                query: { orderId: '*' },
                structure: userOrdersLayout
            }, 'userOrdersGrid');
            userOrdersGrid.startup( );
            var selectHandler = function( event) {
				var ordersStore = new dojo.data.ItemFileWriteStore( { 
					url : "/FinalProject/app/ordersManagement?ordersOp=order_user_orders_list&user=" + this.selection.getSelected()[0].userLogin[0], 
					clearOnClose: true, 
					jsId:"ordersStore"});
				userOrdersGrid.setStore( ordersStore);
            };
            var defineHandler = function( title) {
                return function( ) {
	                if ( !dijit.byId( title)) {
						setTimeout( defineHandler( title), 500);
	                } else {
	                	dojo.connect( dijit.byId( title), "onSelected", selectHandler);
	                }
                }
            };
            setTimeout( defineHandler("disableGrid"), 500);
            setTimeout( defineHandler("activeGrid"), 500);
            setTimeout( defineHandler("waitingGrid"), 500);
		});
	</script>
</html>